package com.qf.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dto.ResultCode;
import com.qf.exceptions.AddDataException;
import com.qf.mapper.AuthorsMapper;
import com.qf.mapper.BookMapper;
import com.qf.mapper.CategoryMapper;
import com.qf.pojo.Authors;
import com.qf.pojo.Book;
import com.qf.pojo.Categories;
import com.qf.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class BookServiceImpl implements BookService {
    private BookMapper bookMapper;
    private CategoryMapper categoryMapper;
    private AuthorsMapper authorsMapper;
    @Autowired
    public void setAuthorsMapper(AuthorsMapper authorsMapper) {
        this.authorsMapper = authorsMapper;
    }

    @Autowired
    public void setCategoryMapper(CategoryMapper categoryMapper) {
        this.categoryMapper = categoryMapper;
    }

    @Autowired
    public void setBookMapper(BookMapper bookMapper) {

        this.bookMapper = bookMapper;
    }

    @Override
    public PageInfo<Book> findAllBooks(int page, int limit, String bookname, int datastatus) {
       
        PageHelper.startPage(page, limit);
        List<Book> books = bookMapper.selectAll(bookname, datastatus);
        for (Book book:books){
            Categories categories = categoryMapper.selectCategoryById(book.getCategoryid());
            Authors authors = authorsMapper.selectAuthorById(book.getAuthorid());
            book.setAuthors(authors);
            book.setCategories(categories);
        }

        PageInfo<Book> pageInfo = new PageInfo<>(books);
        return pageInfo;
    }

    @Override
    public void insertbook(Book book) {
        if(book==null||book.getBookname()==null){
            throw new AddDataException("书籍重要信息不全", ResultCode.NULL_DATA);
        }
        bookMapper.insertBook(book);
    }


}
